import { type ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path';
import developmentConfig from './development.config';
import { createHtmlPlugin } from 'vite-plugin-html'
import autoprefixer from 'autoprefixer'

// https://vite.dev/config/
export default ({ mode }: ConfigEnv) => {
    return {
    base: '/wx/',
    resolve: {
      alias: {
        '@style': path.resolve(__dirname, 'src/common/scss'), 
        '@assets': path.resolve(__dirname, 'src/assets'), 
        '@components': path.resolve(__dirname, 'src/components'), 
        '@store': path.resolve(__dirname, 'src/store'), 
        '@utils': path.resolve(__dirname, 'src/common/ts'), 
        '@views': path.resolve(__dirname, 'src/views'), 
        '@api': path.resolve(__dirname, 'src/api'), 
      },
    },
    server: {
      https: false,
      open: true,
      proxy: {
        '/api': {
          target: developmentConfig.proxyTarget,
          changeOrigin: true,
          rewrite: (path: string)=> path.replace('/^\/api/', ''),
        }
      }
    },
    build: {
      outDir: `target_${mode}`,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: mode === 'PRD',
          drop_debugger: mode === 'PRD',
        }
      }
    },
    plugins: [
      vue(),
      createHtmlPlugin({}),
    ],
    css: {
      postcss: {
        plugins: [
          autoprefixer,
          // 暂时注释postcsspxtoviewport插件以避免类型问题
          // (postcss-px-to-viewport as any)({
          // }),
        ],
      },
      preprocessorOptions: {
      scss: {
        additionalData: `
        @import "@nutui/nutui/dist/styles/variables.scss";
        `
      }
    }
    },

  };


  
}
